<template>
	<view class="index">
		<view v-if="true">
			<!-- 头部 -->
			<u-field v-model="title" label="标题"></u-field>
			<jinEdit placeholder="请输入内容······" :title="title" :draftContent="draftTxt" @edit-content="GetSonEditContent"
				@ImgUrl="GetImgUrl" :username="username"></jinEdit>
			<view>
			</view>
			<u-button type="primary" class="feedback-submit" @click="draft()" :disabled="isDisable">立即投稿</u-button>
			<u-button type="warning" @click="reset()" class="reset">重置</u-button>
		</view>
	</view>
</template>
<script>
	import 'bootstrap/dist/css/bootstrap.min.css';
	import "bootstrap/dist/css/bootstrap.css";
	import jinEdit from '@/components/jin-edit/jin-edit.vue';
	import file from '@/components/l-file/l-file.vue';
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex';
	import util, {
		parGetData,
		parSetData,
		checklogin
	} from '@/common/util';
	import api from '@/util/api.js';
	import request from '@/util/request.js';
	export default {
		data() {
			return {
				format: 'json',
				pageNo: 1,
				pageSize: 1,
				id: '',
				siteId: 'c859d5588b744fe28f83064b037d3890',
				isDisable: false,
				author: 'linker',
				atttitle: '',
				classifyId: '',
				count: '',
				filename: '',
				info: {},
				infoId: '',
				orders: '',
				session: '',
				txt: '',
				txt1: '',
				txt2: '',
				txt3: '',
				username: 'linker',
				title: '',
				SonContent: '',
				introductionList: [],
				ImgUrl: '',
				fileList: [],
				formats: {},
				updata: {},
				action: api.uploadAttachsUrl,
				draftContent: '',
				draftTxt: ''
			};
		},
		onShow() {
			this.username = uni.getStorageSync('username');
			this.token = uni.getStorageSync('token');
			//检查核对
			this.checkstat();
			//检查是否有令牌
			if (!this.token) {
				this.gologinpage()
				this.showlogin = true;
			}
		},
		onLoad() {
			this.$emit('title', this.title);
		},
		onLoad: function(detail, url) {
			if (detail.content != null) {
				this.draftContent = JSON.parse(decodeURIComponent(detail.content));
				this.title = this.draftContent.title;
				this.draftTxt = this.draftContent.txt;
				this.SonContent = this.draftTxt;
			}
		},
		components: {
			jinEdit,
			file
		},
		methods: {
			geturl(res) {
				this.url = res
			},
			// 解决了“[Vue warn]: Avoid mutating a prop directly since,
			// the value will be,overwritten whenever the parent component
			// re-renders. Instead, use a data or computed property based
			//  on the prop's value. Prop being mutated: "***"”报错.
			// 数据的改变在父组件中修改
			onChangeIntroduction(val) {
				this.introductionList = val
			},
			beforeUpload(index, list) {
				console.log('beforeUpload', list);
				this.updata["siteId"] = this.siteId;
				this.updata['Filedata'] = list[0].file;
				this.updata["username"] = uni.getStorageSync('username');
			},
			// 接收内容
			GetSonEditContent(res) {
				this.SonContent = res;
			},
			//接收图片的Url
			GetImgUrl(res) {
				this.ImgUrl = res;
			},
			gologinpage() {
				uni.navigateTo({
					url: '../login/login'
				});
			},
			//统计检查
			checkstat() {
				//检查登录
				var logininfo = util.checklogin();
				this.openid = logininfo.openid;
				this.userinfo = logininfo.userinfo;
			},
			time() {
				this.isDisable = true
				setTimeout(() => {
					this.isDisable = false
				}, 60000)
			},
			//草稿
			draft() {
				//富文本json的转换
				//定义json数据
				if (this.ImgUrl == '') {
					uni.showToast({
						icon: 'none',
						title: '至少需要上传一张图片！'
					})
					return false;
				}
				var parentData = this.SonContent;

				//titlejson的转换
				var TitleData = {
					title: this.title,
					author: this.author,
					titleImg: this.ImgUrl
				};
				var functionTitle = JSON.stringify(TitleData);

				if (!this.SonContent || !this.title) {
					uni.showToast({
						title: '标题或内容不能为空。',
						icon: 'none'
					});
					return false;
				}
				request.postf(api.save, {
					atttitle: this.atttitle,
					classifyId: this.classifyId,
					count: this.count,
					filename: this.filename,
					info: functionTitle,
					infoId: this.infoId,
					orders: this.orders,
					session: this.session,
					txt: parentData,
					txt1: this.txt1,
					txt2: this.txt2,
					txt3: this.txt3,
					titleImg: this.ImgUrl,
					username: this.username
				}).then(response => {
					if (response.data.code === 0) {
						uni.showToast({
							icon: 'none',
							title: '上传成功！',
						})
						this.time();
						this.getList();
					} else {
						uni.showToast({
							icon: 'none',
							title: '上传失败，请稍后重试！'
						})
					}
				}).catch(ex => {
					uni.showToast({
						icon: 'none',
						title: '上传失败，请稍后再试',
					})
				})
			},
			//获取id
			getList() {
				let url = api.listDraft;
				url = url + "?format=" + this.format + "&pageNo=" + this.pageNo + "&pageSize=" + this.pageSize +
					"&session=&username=" + this.username;
				request.getf(url, {
					username: this.username
				}).then(response => {
					if (response.data.code === 0) {
						this.id = response.data.data.list[0].content.id;
						this.release();
					} else {
						uni.showToast({
							icon: 'none',
							title: '请求失败，' + response.data.msg,
						})
					}
				}).catch(ex => {
					console.log('ex=', ex);
					uni.showToast({
						icon: 'none',
						title: '请求失败，请稍后再试'
					})
				})
			},
			reset() {
				this.ImgUrl = '';
				this.SonContent = '';
				this.title = '';
				this.fileList = [];
				this.$router.go(0);
			},
			//投稿
			release() {
				request.put(api.sendToSite, {
					id: this.id,
					siteId: this.siteId,
					username: this.username
				}).then(response => {

				}).catch(ex => {
					uni.showToast({
						icon: 'none',
						title: '请求失败，请稍后再试'
					})
				})
			},
			//上传附件
			fujian() {
				uni.chooseFile({
					count: 90,
					extension: ['.jpg', '.zip', '.doc', '.xls', 'ppt', '.pdf', '.docx', '.xlsx', '.pptx', 'txt'],
					success: function(res) {
						console.log(res.tempFiles);
						const tempFilePaths = res.tempFiles;
					}
				});
				console.log("fujian=", this.tempFilePaths);
				request.fujianpost(api.uploadAttachs, {
					username: this.username,
					Filedata: this.tempFilePaths,
					siteId: this.siteId
				}).then(response => {
					console.log('response=', response);
					if (!Filedata) {
						uni.showToast({
							title: '附件不能为空！',
							icon: 'none'
						});
						return false;
					}
					if (response.data.code === 0) {
						uni.showToast({
							icon: 'success',
							title: '上传成功！',
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: '上传失败，请稍后重试！'
						})
					}
				}).catch(ex => {
					uni.showToast({
						icon: 'none',
						title: '附件上传失败，请稍后再试',
					})
				})
			},
			chooseVideo: function() {
				var self = this;
				uni.chooseVideo({
					sourceType: ['album', 'camera'],
					success: res => {
						self.video = res.tempFilePath;
					}
				});
			},
			/* 登录方法 */
			login(res) {
				util.logincomm(res, this);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.index {
		background: #f9f9f9;
		overflow-x: hidden;
	}

	.index .textcontent textarea {
		display: block;
		height: 150rpx;
		margin: 10rpx;
	}

	#showlogin {
		position: fixed;
		width: 40%;
		left: 50%;
		margin-left: -20%;
		z-index: 999;
		top: 200rpx;
	}

	/* 上传 */
	.uni-uploader {
		flex-direction: column;
		width: 100%;
		padding: 0;
		background: #fff;
		margin: 15rpx auto;
	}

	.image-plus-text {
		font-size: 20rpx;
		color: #777;
		margin: 0 auto;
	}

	.uni-uploader-body {
		margin-top: 0;
		overflow: hidden;
	}

	.uni-uploader__file {
		float: left;
		margin: auto 15rpx;
		padding-right: 15rpx;
		padding-top: 0;
		position: relative;
	}

	.uni-uploader__file .iconfont {
		position: absolute;
		right: -3rpx;
		top: -10rpx;
		font-size: 20rpx;
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		background: rgba(0, 0, 0, 0.5);
		color: #fff;
		text-align: center;
		line-height: 30rpx;
		justify-content: center;
	}

	.uni-uploader__img {
		display: block;
		width: 158rpx;
		height: 158rpx;
	}

	.uni-uploader__video {
		display: block;
		width: 158rpx;
		height: 158rpx;
	}

	.uni-uploader__input-box {
		float: left;
		position: relative;
		margin-top: 8rpx;
		margin-right: 18rpx;
		margin-bottom: 18rpx;
		margin-left: 8rpx;
		width: 200rpx;
		height: 200rpx;
		display: flex;
		align-items: flex-end;
		justify-content: center;
		border-color: #f1f1f1;
	}

	.uni-uploader__input-box:before,

	.uni-uploader__input-box:after {
		content: ' ';
		position: absolute;
		top: 45%;
		left: 50%;
		transform: translate(-50%, -50%);
		background-color: #eee;
	}

	.uni-uploader__input-box:before {
		width: 4rpx;
		height: 79rpx;
	}

	.uni-uploader__input-box:after {
		width: 79rpx;
		height: 4rpx;
	}

	.uni-uploader__input {
		position: absolute;
		z-index: 1;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}

	.u-p_b {
		color: #007AFF;
		font-size: 20px;
		margin-top: 15px;
		text-align: right;
		margin-left: 440px;
	}

	a:link {
		text-decoration: none;
	}

	.u-fujian {
		color: #007AFF;
		font-size: 20px;
		margin-top: 15px;
		text-align: left;
		margin-left: 20px;
	}

	.reset {
		width: 94%;
		height: 75px;
		bottom: 0px;
		left: 20px;
	}
</style>
